$(function () {
  var isPwdOk = false;
  var isPhoneOk = false;
  var isCodeOk = false;
  var isRegisterSuccess = false; // 记录是否注册成功

  $(".confirm").click(function () {

    //验证手机号
    var phone = $("#phone").val();
    console.log(phone);
    var phoneReg = /^1[3-9]\d{9}$/;

    if (phoneReg.test(phone)) {
      // setCookie('phone', phone, 7, path = "/");
      isPhoneOk = true;
      isExistPhone({ phone }).then(data => {
        var { status, msg } = data;
        if (status) {
          $(".tips").text("* 手机号尚未注册!");
          // setCookie('phone', phone, 7, path = "/");
          isPhoneOk = true;
        } else {
          $(".tips").text("* 手机号已注册!");
        }
      })

    } else {
      $(".tips").text("请输入正确的手机号");
    }




    //验证密码
    var pwd = $("#pwd").val();
    console.log(pwd);
    var pwdReg = /^[\w$]{6,12}$/;

    if (pwdReg.test(pwd)) {
      // setCookie('pwd', pwd, 7, path = "/");
      isPwdOk = true;
    } else {
      $(".tips").text("密码不在合法范围内");
    }


    //验证验证码
    var code = $("#codeInp").val();
    console.log(code);

    var RCode = $("#createCode").text();
    // console.log(RCode); //随机的验证码
    var codeReg = new RegExp(`^${RCode}$`, "i");

    if (codeReg.test(code)) {

      isCodeOk = true;
    } else {
      $(".tips").text("验证码错误");
    }


    if (phone == '' && pwd == '' && code == '') {
      $(".tips").text("请输入手机号");
    } else if (pwd == '' && code == '') {
      $(".tips").text("请输入密码");

    } else if (code == '') {
      $(".tips").text("请输入验证码");

    }

    if (isRegisterSuccess) return false;
    if (isPwdOk && isPhoneOk && isCodeOk) {
      // location.href = "login.html";
      registered({ phone, pwd }).then(data => {
        console.log(data);
        var { status, msg } = data;
        if (status) {
          isRegisterSuccess = true;
          alert('注册成功');
          location.href = "login.html";
        } else {
          isRegisterSuccess = false;
        }
      })

    }


  })


  $(".go_login").click(function () {
    $(location).attr("href", "login.html")
    console.log(bo);
  })

})
